package com.djq.hhc.handle;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: dadd7
 * @Date: 2023/03/02/下午 4:50
 * @Description: 自动填充
 */

@Component
public class FieldFillHandle implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        Object created = getFieldValByName("createTime", metaObject);
        if (null == created) {
            //字段为空，可以进行填充
            setFieldValByName("createTime", new Date(), metaObject);
        }

        Object updated = getFieldValByName("updateTime", metaObject);
        if (null == updated) {
            //字段为空，可以进行填充
            setFieldValByName("updateTime", new Date(), metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        //更新数据时，直接更新字段
        setFieldValByName("updateTime", new Date(), metaObject);
    }
}
